package com.qlu.syq.service.admin.impl;
/**
 * 入住管理service实现类
 */
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.qlu.syq.aspectj.DataScope;
import com.qlu.syq.dao.BookOrderDao;
import com.qlu.syq.dao.RoomTypeDao;
import com.qlu.syq.dao.admin.RoomDao;
import com.qlu.syq.entity.BookOrder;
import com.qlu.syq.entity.RoomType;
import com.qlu.syq.entity.admin.Room;
import com.qlu.syq.enums.BookOrderStatus;
import com.qlu.syq.enums.CheckinStatus;
import com.qlu.syq.enums.RoomStatus;
import com.qlu.syq.enums.RoomTypeStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qlu.syq.dao.admin.CheckinDao;
import com.qlu.syq.entity.admin.Checkin;
import com.qlu.syq.service.admin.CheckinService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CheckinServiceImpl implements CheckinService {

	@Autowired
	private CheckinDao checkinDao;
	@Autowired
	private BookOrderDao bookOrderDao;
	@Autowired
	private RoomTypeDao roomTypeDao;
	@Autowired
	private RoomDao roomDao;

	@Override
	@Transactional
	public int add(Checkin checkin, Long bookOrderId) {
		// 查询对应房型
		RoomType roomType = roomTypeDao.find(checkin.getRoomTypeId());

		// 从预定来的入住单(入住既可以是直接入住也可以是已经预定的人来入住)
		if (bookOrderId != null) {
			// 修改预定状态为已入住
			BookOrder bookOrder = bookOrderDao.find(bookOrderId);
			bookOrder.setStatus(BookOrderStatus.USING);
			bookOrderDao.edit(bookOrder);
			roomType.setBookNum(roomType.getBookNum() - 1);//预定数减1
		} else {
			// 直接入住客户
			roomType.setAvilableNum(roomType.getAvilableNum() - 1);
		}
		//  入住成功后去修改该房型的预定数
		roomType.setLivedNum(roomType.getLivedNum() + 1);//入住数加1
		roomTypeDao.updateNum(roomType);
		// 如果可用的房间数为0，则设置该房型状态已满
		if (roomType.getAvilableNum() == 0) {
			roomType.setStatus(RoomTypeStatus.FULL);
			roomTypeDao.edit(roomType);
		}
		Room room = roomDao.find(checkin.getRoomId());
		if (room != null) {
			//要把房间状态设置为已入住
			room.setStatus(RoomStatus.USING);
			roomDao.edit(room);
		}

		checkin.setCreateTime(new Date());
		// 添加入住记录
		return checkinDao.add(checkin);
	}

	@Override
	public int edit(Checkin checkin) {
		return checkinDao.edit(checkin);
	}

	@Override
	public int delete(Long id) {
		return checkinDao.delete(id);
	}

	@Override
	@DataScope(alias = "f")
	public List<Checkin> findList(Map<String, Object> queryMap) {
		return checkinDao.findList(queryMap);
	}

	@Override
	@DataScope(alias = "f")
	public Integer getTotal(Map<String, Object> queryMap) {
		return checkinDao.getTotal(queryMap);
	}

	@Override
	public Checkin find(Long id) {
		return checkinDao.find(id);
	}

	@Override
	public List<Map> getStatsByMonth() {
		return checkinDao.getStatsByMonth();
	}

	@Override
	public List<Map> getStatsByDay() {
		return checkinDao.getStatsByDay();
	}

	@Override
	@Transactional
	public int checkout(Long checkId) {
		Checkin checkin = checkinDao.find(checkId);

		checkin.setStatus(CheckinStatus.SETTLED);

		//首先操作房间状态
		Room room = roomDao.find(checkin.getRoomId());
		if (room != null) {
			// 设置房间为打扫中
			room.setStatus(RoomStatus.CLEANING);
			roomDao.edit(room);
		}
		//其次修改房型可用数、入住数、状态
		RoomType roomType = roomTypeDao.find(checkin.getRoomTypeId());
		if (roomType != null) {
			roomType.setAvilableNum(roomType.getAvilableNum() + 1);
			if (roomType.getAvilableNum() > roomType.getRoomNum()) {
				roomType.setAvilableNum(roomType.getRoomNum());
			}
			roomType.setLivedNum(roomType.getLivedNum() - 1);
			if (roomType.getStatus() == RoomTypeStatus.FULL) {
				roomType.setStatus(RoomTypeStatus.FREE);
			}
			// 判断是否来自预定
			if (checkin.getBookOrderId() != null) {
				// 设置预约记录为结算
				BookOrder bookOrder = bookOrderDao.find(checkin.getBookOrderId());
				bookOrder.setStatus(BookOrderStatus.SETTLED);
				bookOrderDao.edit(bookOrder);
			}
			roomTypeDao.updateNum(roomType);
			roomTypeDao.edit(roomType);
		}
		return checkinDao.edit(checkin);

	}
}
